package com.isoft.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.xml.bind.annotation.*;
import lombok.Data;

import java.io.Serializable;

/**
 * DFME-AFID消息实体类
 * 对应XML报文结构
 */
@Data
@XmlRootElement(name = "MSG")
@XmlAccessorType(XmlAccessType.FIELD)
public class DfmeAfidEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @JsonProperty("meta")
    @XmlElement(name = "META", required = true)
    private Meta meta;

    @JsonProperty("dflt")
    @XmlElement(name = "DFLT", required = true)
    private Dflt dflt;

    // 用于接收数据库生成的主键ID
    @XmlTransient  // 不参与XML序列化
    private Long msgId;

    /**
     * 元数据内部类
     */
    @Data
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Meta implements Serializable {
        private static final long serialVersionUID = 1L;

        @XmlElement(name = "SNDR", required = true)
        private String sender;  // 发送方

        @XmlElement(name = "RCVR")
        private String receiver;  // 接收方

        @XmlElement(name = "SEQN", required = true)
        private Long sequenceNumber;  // 序列号

        @XmlElement(name = "DDTM", required = true)
        private String dateTime;  // 日期时间(yyyyMMddHHmmss格式)

        @XmlElement(name = "TYPE", required = true)
        private String type;  // 消息类型

        @XmlElement(name = "STYP", required = true)
        private String subType;  // 子类型
        
        // 添加msgId字段用于自增主键
        private Long msgId;
        
        public Long getMsgId() {
            return msgId;
        }
        
        public void setMsgId(Long msgId) {
            this.msgId = msgId;
        }
    }

    /**
     * 业务数据内部类
     */
    @Data
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Dflt implements Serializable {
        private static final long serialVersionUID = 1L;

        @XmlElement(name = "FLID", required = true)
        private Long flightId;  // 航班ID

        @XmlElement(name = "FFID", required = true)
        private String flightFileId;  // 航班文件ID

        @XmlElement(name = "FIDE", required = true)
        private String flightIdentifier;  // 航班标识符

        @XmlElement(name = "AFID")
        private Long alternateFlightId;  // 备降航班ID
    }

    // 以下是新增的实用方法

    /**
     * 获取格式化后的日期时间
     * @return 格式化的日期时间字符串(yyyy-MM-dd HH:mm:ss)
     */
    public String getFormattedDateTime() {
        if (meta != null && meta.dateTime != null && meta.dateTime.length() == 14) {
            return meta.dateTime.substring(0, 4) + "-" +
                    meta.dateTime.substring(4, 6) + "-" +
                    meta.dateTime.substring(6, 8) + " " +
                    meta.dateTime.substring(8, 10) + ":" +
                    meta.dateTime.substring(10, 12) + ":" +
                    meta.dateTime.substring(12, 14);
        }
        return meta != null ? meta.dateTime : null;
    }

    /**
     * 判断是否是有效消息
     * @return true如果所有必填字段都存在
     */
    public boolean isValid() {
        return meta != null &&
                meta.sender != null &&
                meta.sequenceNumber != null &&
                meta.dateTime != null &&
                meta.type != null &&
                meta.subType != null &&
                dflt != null &&
                dflt.flightId != null &&
                dflt.flightFileId != null &&
                dflt.flightIdentifier != null;
    }

    /**
     * 获取消息类型全称
     * @return TYPE-STYP格式的全称
     */
    public String getFullMessageType() {
        return meta != null ? meta.type + "-" + meta.subType : null;
    }

    public Meta getMeta() {
        return meta;
    }

    public void setMeta(Meta meta) {
        this.meta = meta;
    }

    public Dflt getDflt() {
        return dflt;
    }

    public void setDflt(Dflt dflt) {
        this.dflt = dflt;
    }

    public Long getMsgId() {
        return msgId;
    }

    public void setMsgId(Long msgId) {
        this.msgId = msgId;
    }

    public Long getFlightId() {
        return dflt.flightId;
    }

    public void setFlightId(Long flightId) {
        dflt.flightId = flightId;
    }
}